<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;

use App\Http\Models\CacheModel;

class TemplateModel extends Model
{
    const CACHE_KEY = 'template:';
	protected $connection = 'db_main';

	protected $table = 'wp_gcreator_template';
	//protected $fillable = ['action_id', 'remark', 'uid', 'model', 'action_ip', 'create_time'];

	public $timestamps = false;

    // info by name
    public static function info($name)
    {
        if ($info = CacheModel::get(self::CACHE_KEY . $name))
        {
            return json_decode($info);
        } else {
            $info = self::where(["name" => $name]) ->first();
            if($info)
            {
                CacheModel::set(self::CACHE_KEY . $name, json_encode($info));
            }
            return $info;
        }
    }

    // info by id
    public static function info_by_id($id)
    {
        if ($info = CacheModel::get(self::CACHE_KEY . $id))
        {
            return json_decode($info);
        } else {
            $info = self::where(["id" => $id]) ->first();
            if($info)
            {
                CacheModel::set(self::CACHE_KEY . $id, json_encode($info));
            }
            return $info;
        }
    }
}

